//
//  LCFloatingWindowAnimator.m
//  LC
//
//  Created by admin on 17/5/22.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "LCFloatingWindowAnimator.h"
#import "LCFloatingWindow.h"
#import "Masonry/Masonry.h"


@interface LCFloatingWindowAnimator ()<UIViewControllerAnimatedTransitioning>

@end

@implementation LCFloatingWindowAnimator {
    BOOL    _isPresented;
}

#pragma mark - UIViewControllerTransitioningDelegate -
// 当界面显示转场回调,提供一个实现了显示转场协议的对象
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    _isPresented = YES;
    return self;
}

// 当界面消失转场,提供一个实现了消失转场协议的对象
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    _isPresented = NO;
    return self;
}

#pragma mark - UIViewControllerAnimatedTransitioning -
// 提供转场时间
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext {
    return 0.25;
}

// 提供转场动画
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
    // 获取转场的开始视图
    UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
    
    // 转场的目标视图
    UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
    
    // 一般 转场切换的两个视图应该共存在一个容器视图中
    UIView *containerView = [transitionContext containerView];
    // containerView 中只有 fromView 对象, 没 toView 的视图对象
    // 把 toView 视图对象添加到容器视图中
    
    
    if(_isPresented) {
        // 当显示的时候 目标视图就应该在 源视图上所以使用 addSubview
        [containerView addSubview:toView];
        if(self.contentView == nil) {
            toView.frame = CGRectMake(containerView.frame.size.width, 0, toView.frame.size.width, toView.frame.size.height);
            [UIView animateWithDuration:0.25
                             animations:^{
                                 toView.frame = CGRectMake(0, 0, toView.frame.size.width, toView.frame.size.height);
                             } completion:^(BOOL finished) {
                                 toView.frame = CGRectMake(0, 0, toView.frame.size.width, toView.frame.size.height);
                                 
                                 // 一旦动画转场成功
                                 [transitionContext completeTransition:YES];
                             }];
        } else {
            
            // 获取动画开始的位置
            CGRect fromRect = [self.contentView convertRect:self.contentView.frame toView:containerView];
            
            [self.contentView removeFromSuperview];
            
            [containerView addSubview:self.contentView];
            
            NSLog(@"fromRect:%@",NSStringFromCGRect(fromRect));
            
            [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(fromRect.origin.y);
                make.left.mas_equalTo(fromRect.origin.x);
                make.size.mas_equalTo(fromRect.size);
            }];
            self.contentView.frame = fromRect;
            
            NSLog(@"destRect:%@",NSStringFromCGRect(_destRect));
            
            toView.alpha = 0.0;
            
            [UIView animateWithDuration:0.25
                             animations:^{
                                 toView.alpha = 1.0;
                                 [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
                                     make.top.mas_equalTo(_destRect.origin.y);
                                     make.left.mas_equalTo(_destRect.origin.x);
                                     make.size.mas_equalTo(_destRect.size);
                                 }];
                                 self.contentView.frame = _destRect;
                             } completion:^(BOOL finished) {
                                 [self.contentView removeFromSuperview];
                                 toView.alpha = 1.0;
                                 // 一旦动画转场成功
                                 [transitionContext completeTransition:YES];
                                 if(self.showAnimationCompletion) {
                                     self.showAnimationCompletion();
                                 }
                             }];
        }
        
    } else {
        // 当退出的时 目标视图(上一个视图) 应该在 源视图之下, 所以需要 insertSubview:atIndex:
//        [LCFloatingWindow sharedInstance].contentView
        [containerView insertSubview:toView atIndex:0];
        
        // 获取动画开始的位置
        CGRect fromRect = [self.contentView convertRect:self.contentView.frame toView:containerView];
        NSLog(@"fromRect:%@",NSStringFromCGRect(fromRect));
        
        // 把当前内容视图从 视图控制器 中移除
        [self.contentView removeFromSuperview];
        
        // 并且把内容视图 添加到 当前容器视图中
        [containerView addSubview:self.contentView];
        
        
        
        // 需要设置内容视图的目标位置
        [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(fromRect.origin.y);
            make.left.mas_equalTo(fromRect.origin.x);
            make.size.mas_equalTo(fromRect.size);
        }];
        
        // 设置开始的frame
        self.contentView.frame = fromRect;
        
        
        [UIView animateWithDuration:0.25 animations:^{
            fromView.alpha = 0.0;
            [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(_destRect.origin.y);
                make.left.mas_equalTo(_destRect.origin.x);
                make.size.mas_equalTo(_destRect.size);
            }];
            self.contentView.frame = _destRect;
            
        } completion:^(BOOL finished) {
            fromView.alpha = 0.0;
            [transitionContext completeTransition:YES];
            if(self.dismissAnimationCompletion) {
                self.dismissAnimationCompletion();
            }
        }];
    }
}

@end
